package com.yizisu.playerlibrary.view.video_view.view

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import com.yizisu.playerlibrary.IYzsPlayer
import com.yizisu.playerlibrary.view.video_view.helper.IPlayerConfig
import com.yizisu.playerlibrary.view.video_view.helper.IPlayerHelper
import com.yizisu.playerlibrary.view.video_view.helper.dip

/**
 * 展示视频画面，继承手势操作类
 */
open class PlayerPlayPauseView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(
    context,
    attrs,
    defStyleAttr
), IPlayerHelper {

    //是否在播放的时候隐藏按钮
    var enableGoneWhenPlaying = true

    private val eNPlayView by lazy {
        ENPlayView(context).apply {
            setLineWidth(dip(1.6f))
            setBgLineWidth(dip(1.6f))
            addView(
                this,
                FrameLayout.LayoutParams(
                    FrameLayout.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
                )
            )
        }
    }

    override fun onAttachPlayerConfig(playerConfig: IPlayerConfig) {
        super.onAttachPlayerConfig(playerConfig)
        eNPlayView
        setOnClickListener {
            val player = playerConfig.player
            if (player != null) {
                if (player.isPlaying()) {
                    player.pause()
                } else {
                    player.play()
                }
            }
        }
    }


    override fun onPlay(playStatus: Boolean, playerModel: IYzsPlayer.Model?) {
        super.onPlay(playStatus, playerModel)
        if (playStatus) {
            eNPlayView.play {
                if (enableGoneWhenPlaying) {
                    postDelayed({
                        this.visibility = View.INVISIBLE
                    }, 200)
                }
            }
        }
    }

    override fun onPause(playStatus: Boolean, playerModel: IYzsPlayer.Model?) {
        super.onPause(playStatus, playerModel)
        if (!playStatus) {//播放暂停
            this.visibility = View.VISIBLE
            eNPlayView.pause()
        }
    }

    override fun isNeedPlayerListener(): Boolean {
        return true
    }
}